import axios from 'axios';
import config from '../config';

// 定义登录接口参数类型
export interface LoginParams {
  email: string;
  password: string;
}

// 定义登录接口返回类型
export interface LoginResponse {
  success: boolean;
  message?: string;
  token?: string;
  user?: {
    email: string;
    nickname?: string;
  };
}

// 定义注册接口参数类型
export interface RegisterParams {
  email: string;
  password: string;
  nickname: string;
  verificationCode: string;
}

// 定义注册接口返回类型
export interface RegisterResponse {
  success: boolean;
  message?: string;
  token?: string;
  user?: {
    email: string;
    nickname: string;
  };
}

// 定义发送验证码接口参数类型
export interface SendCodeParams {
  email: string;
}

// 定义发送验证码接口返回类型
export interface SendCodeResponse {
  success: boolean;
  message?: string;
}

// 登录接口
export const login = async (params: LoginParams): Promise<LoginResponse> => {
  try {
    const response = await axios.post(`${config.baseUrl}/login`, params);
    return response.data;
  } catch (error) {
    console.error('登录失败:', error);
    return {
      success: false,
      message: '登录失败，请稍后重试'
    };
  }
};

// 注册接口
export const register = async (params: RegisterParams): Promise<RegisterResponse> => {
  try {
    const response = await axios.post(`${config.baseUrl}/register`, params);
    return response.data;
  } catch (error) {
    console.error('注册失败:', error);
    return {
      success: false,
      message: '注册失败，请稍后重试'
    };
  }
};

// 发送验证码接口
export const sendVerificationCode = async (params: SendCodeParams): Promise<SendCodeResponse> => {
  try {
    const response = await axios.post(`${config.baseUrl}/send-verification-code`, params);
    return response.data;
  } catch (error) {
    console.error('发送验证码失败:', error);
    return {
      success: false,
      message: '发送验证码失败，请稍后重试'
    };
  }
};